//
//  DetailOfHeaderReusableView.m
//  PetAPP
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "DetailOfHeaderReusableView.h"
#import "ModelOfDetailOfPage.h"
#import <YYWebImage.h>

@interface DetailOfHeaderReusableView () <UIScrollViewDelegate>

@property (nonatomic, retain) UIView *userView;

@property (nonatomic, retain) UIView *picsView;

@property (nonatomic, retain) UIView *messageView;

@property (nonatomic, retain) UIView *tagView;

@property (nonatomic, retain) UIView *likeUserView;

@property (nonatomic, retain) UIView *timeAndLookView;

@property (nonatomic, assign) CGFloat headerHight;

@property (nonatomic, retain) UIImageView *viewOfUserFace;

@property (nonatomic, retain) UILabel *labelOfAuthor;

@property (nonatomic, retain) UILabel *labelOfAddress;

@property (nonatomic, retain) UIButton *buttonOfisfollow;

@property (nonatomic, retain) UILabel *labelOfPicNum;

@end


@implementation DetailOfHeaderReusableView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
        self.backgroundColor = [UIColor whiteColor];
    }
    
    return self;
}

- (void)createSubViews {
#pragma mark - 创建用户信息的view
    self.userView  = [[UIView alloc] init];
    [self addSubview:self.userView];
    [_userView release];
    
    
    
    self.viewOfUserFace = [[UIImageView alloc] init];
    [self.userView addSubview:self.viewOfUserFace];
    [_viewOfUserFace release];
    
    
    self.labelOfAuthor = [[UILabel alloc] init];
    [self.userView addSubview:self.labelOfAuthor];
    [_labelOfAuthor release];
    self.labelOfAuthor.font = [UIFont systemFontOfSize:17];
    
    
    self.labelOfAddress = [[UILabel alloc] init];
    [self.userView addSubview:self.labelOfAddress];
    [_labelOfAddress release];
    self.labelOfAddress.textColor = [UIColor grayColor];
    self.labelOfAddress.font = [UIFont systemFontOfSize:14];
    
    
    self.buttonOfisfollow = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.userView addSubview:self.buttonOfisfollow];
    self.buttonOfisfollow.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.buttonOfisfollow setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    
    
#pragma mark ** 创建图片的view
    self.picsView = [[UIView alloc] init];
    [self addSubview:self.picsView];
    [_picsView release];
    self.picsView.backgroundColor = [UIColor redColor];

#pragma mark ** 创建内容的view
    self.messageView = [[UIView alloc] init];
    [self addSubview:self.messageView];
    [_messageView release];
    

#pragma mark ** 创建标签的view
    self.tagView = [[UIView alloc] init];
    [self addSubview:self.tagView];
    [_tagView release];
   
    
    UIImageView *imageViewOfMark = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 20, 20)];
    imageViewOfMark.image = [UIImage imageNamed:@"biaoqian"];
    for (int i = 0 ; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.tagView addSubview:button];
        button.titleLabel.font = [UIFont systemFontOfSize:17];
        [button setTitleColor:[UIColor colorWithRed:0.0796 green:0.2411 blue:0.9941 alpha:1.0]forState:UIControlStateNormal];
        button.tag = i + 10;
    }
    [self.tagView addSubview:imageViewOfMark];
    [imageViewOfMark release];
    
    
    

#pragma mark ** 创建喜欢用户头像的view
    self.likeUserView = [[UIView alloc] init];
    [self addSubview:self.likeUserView];
    [_likeUserView release];
   
    
    UIImageView *likeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"like"]];
    [self.likeUserView addSubview:likeImageView];
    [likeImageView release];
    likeImageView.frame = CGRectMake(10, 15, 20, 20);
    
    

#pragma mark ** 创建时间和观看数量的view
    self.timeAndLookView = [[UIView alloc] init];
    [self addSubview:self.timeAndLookView];
    [_timeAndLookView release];
    


}
#pragma mark - 通过model赋值
- (void)handleDataWithModel:(ModelOfDetailOfPage *)model {
    CGFloat width = self.frame.size.width;
#pragma mark ** 显示用户信息的view
    self.userView.frame = CGRectMake(0, 0, width, 50);
    
    self.viewOfUserFace.frame = CGRectMake(10, 5, 40, 40);
    self.viewOfUserFace.layer.cornerRadius = self.viewOfUserFace.frame.size.width / 2;
    self.viewOfUserFace.layer.masksToBounds = YES;
    self.viewOfUserFace.yy_imageURL = [NSURL URLWithString:model.userface];
    
    
    self.labelOfAuthor.frame = CGRectMake(60, 5, 150, 20);
    self.labelOfAuthor.text = model.author;
    
    
    self.labelOfAddress.frame = CGRectMake(60, 26, 150, 19);
    self.labelOfAddress.text = model.address;
    
    
    self.buttonOfisfollow.frame = CGRectMake(width - (15 + 80), 10, 80, 30);
    self.buttonOfisfollow.layer.cornerRadius = 4;
    if (model.isfollow == 0) {/**< 已经关注 */
        self.buttonOfisfollow.backgroundColor = [UIColor whiteColor];
        self.buttonOfisfollow.layer.borderWidth = 0;
        [self.buttonOfisfollow setTitle:@"已关注" forState:UIControlStateNormal];
    }else {
        self.buttonOfisfollow.backgroundColor = [UIColor colorWithRed:1.0 green:0.8423 blue:0.2839 alpha:1.0];
        self.buttonOfisfollow.layer.borderWidth = 0;
        [self.buttonOfisfollow setTitle:@"✚ 关注" forState:UIControlStateNormal];
    }
    
#pragma mark ** 显示图片的view
    self.picsView.frame = CGRectMake(0, self.userView.frame.origin.y + self.userView.frame.size.height, width, 400);
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:(CGRect){.size = self.picsView.frame.size}];
    [self.picsView addSubview:scroll];
    [scroll release];
    scroll.tag = 100;
    scroll.delegate = self;
    CGFloat scrollWidth = scroll.frame.size.width;
    scroll.backgroundColor = [UIColor grayColor];
    /** 设置scroll大小 */
    scroll.contentSize = CGSizeMake(scrollWidth * model.pics.count, scroll.frame.size.height);
    
    for (int i = 0; i < model.pics.count; i++) {
        UIImageView *picImageView = [[UIImageView alloc] init];
        picImageView.yy_imageURL = [NSURL URLWithString:[model.pics[i] objectForKey:@"content"]];
        picImageView.frame = CGRectMake(i * scrollWidth, 0, scrollWidth, scroll.frame.size.height);
        [scroll addSubview:picImageView];
        [picImageView release];
        
    }
    scroll.pagingEnabled = YES;
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.bounces = NO;
    
    self.labelOfPicNum = [[UILabel alloc] initWithFrame:CGRectMake(self.picsView.frame.size.width - 60, self.picsView.frame.size.height - 30, 50, 20)];
    [self.picsView addSubview:self.labelOfPicNum];
    [_labelOfPicNum release];
    self.labelOfPicNum.text = [NSString stringWithFormat:@"1/%ld",model.pics.count];
    self.labelOfPicNum.textAlignment = NSTextAlignmentCenter;
    self.labelOfPicNum.textColor = [UIColor whiteColor];
    
    
    
    UIBezierPath *backgroundPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 3, 50, 20) cornerRadius:20];
    
    self.labelOfPicNum.layer.shadowPath = backgroundPath.CGPath;
    self.labelOfPicNum.layer.shadowOpacity = 0.4;
    self.labelOfPicNum.layer.shadowRadius = 0;
    self.labelOfPicNum.layer.shadowColor = [UIColor blackColor].CGColor;
    
    
#pragma mark ** 显示内容的view
    /** 上方的内容 */
    UILabel *labelOfSubject = [[UILabel alloc] init];
    labelOfSubject.numberOfLines = 0;
    labelOfSubject.text = model.subject;
    CGRect messageRect = [model.subject boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20]} context:nil];
    labelOfSubject.frame = CGRectMake(15, 10, [UIScreen mainScreen].bounds.size.width - 30 , messageRect.size.height);
    [self.messageView addSubview:labelOfSubject];
    [labelOfSubject release];
    labelOfSubject.font = [UIFont systemFontOfSize:20];
    labelOfSubject.textColor = [UIColor colorWithRed:0.401 green:0.2705 blue:0.1367 alpha:1.0];
    
    
    /** 下方的内容 */
    UILabel *labelOfContent = [[UILabel alloc] init];
    labelOfContent.numberOfLines = 0;
    labelOfContent.text = model.content;
    CGRect contentRect = [model.content boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20]} context:nil];
    labelOfContent.frame = CGRectMake(15, labelOfSubject.frame.origin.y + labelOfSubject.frame.size.height + 10, [UIScreen mainScreen].bounds.size.width - 30, contentRect.size.height);
    [self.messageView addSubview:labelOfContent];
    [labelOfContent release];
    labelOfContent.font = [UIFont systemFontOfSize:20];
    
    self.messageView.frame = CGRectMake(0, self.picsView.frame.origin.y + self.picsView.frame.size.height, width, labelOfSubject.frame.size.height + labelOfContent.frame.size.height + 30);
#pragma mark ** 显示标签的view
    self.tagView.frame = CGRectMake(0, self.messageView.frame.origin.y + self.messageView.frame.size.height, width, 20);
    
    NSArray *tagArr = model.tags;
    for (int i = 0; i < tagArr.count; i++) {
        NSString *str = [tagArr[i] objectForKey:@"tagname"];
        
        CGRect buttonFrame = [str boundingRectWithSize:CGSizeMake(0, 18) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
        
        UIButton *button = [self viewWithTag:(i + 10)];
        UIButton *lastButton = [self viewWithTag:(i + 9)];
        [button setTitle:str forState:UIControlStateNormal];
        if (i != 0) {
            button.frame = CGRectMake(lastButton.frame.origin.x + lastButton.frame.size.width + i * 5, 0, buttonFrame.size.width + 10, 20);
        }else {
            button.frame = CGRectMake(35, 0, buttonFrame.size.width + 10, 20);
        }
        
    }


#pragma mark ** 显示喜欢用户头像的view
    self.likeUserView.frame = CGRectMake(0, self.tagView.frame.origin.y + self.tagView.frame.size.height + 10,width , 50);
    for (int i = 0; i < model.recommendArr.count; i++) {
        if (i < 9) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(35 + i * 40 + i * 10, 5, 40, 40)];
            [self.likeUserView addSubview:imageView];
            [imageView release];
            imageView.tag = i + 10;
            imageView.layer.cornerRadius = imageView.frame.size.width / 2;
            imageView.layer.masksToBounds = YES;
            imageView.yy_imageURL = [NSURL URLWithString:[model.recommendArr[i] objectForKey:@"userface"]];
            
        }
       
    }
    
    
    
#pragma mark ** 显示时间和观看数量的view
    /** 设置观看图层的大小 */
    self.timeAndLookView.frame = CGRectMake(0, self.likeUserView.frame.origin.y + self.likeUserView.frame.size.height + 10,width , 30);
    /** 设置时间图标 */
    UIImageView *timeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shijian"]];
    timeImageView.frame = CGRectMake(10, 5, 20, 20);
    [self.timeAndLookView addSubview:timeImageView];
    [timeImageView release];
    
    UILabel *labelOftime = [[UILabel alloc] init];
    [self.timeAndLookView addSubview:labelOftime];
    [labelOftime release];
    labelOftime.textColor = [UIColor grayColor];
    labelOftime.font = [UIFont systemFontOfSize:15];
    
    /** 计算多久之前的帖子 */
    NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:model.dateline.intValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *strDate = [dateFormatter stringFromDate:tempDate];
    [dateFormatter setDateFormat:@"MM-dd"];
    NSString *str2Date = [dateFormatter stringFromDate:tempDate];
    [dateFormatter release];
    
    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSince1970];
    float seconds = (int)time - model.dateline.doubleValue;
    float points = seconds / 60;
    if (points < 60) {
        labelOftime.text = [NSString stringWithFormat:@"%d分钟前", (int)points];
        
    }else if (points > 60 && points < 3600) {
        
        labelOftime.text = [NSString stringWithFormat:@"%@",strDate];
    }else {
        labelOftime.text = [NSString stringWithFormat:@"%@",str2Date];
    }
    
    CGRect dateText = [labelOftime.text boundingRectWithSize:CGSizeMake(0, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil];
    labelOftime.frame = CGRectMake(35, 5, dateText.size.width, 20);
    
    
    /** 观看数量图标 */
    UIImageView *lookImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guankan"]];
    lookImageView.frame = CGRectMake(labelOftime.frame.size.width + labelOftime.frame.origin.x + 10, 5, 20, 20);
    [self.timeAndLookView addSubview:lookImageView];
    [lookImageView release];
    
    UILabel *labelOfViews = [[UILabel alloc] initWithFrame:CGRectMake(lookImageView.frame.size.width + lookImageView.frame.origin.x + 5, 5, 40, 20)];
    [self.timeAndLookView addSubview:labelOfViews];
    [labelOfViews release];
    labelOfViews.text = model.views;
    labelOfViews.textColor = [UIColor grayColor];
    labelOfViews.font = [UIFont systemFontOfSize:15];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    UIScrollView *scroll = [self viewWithTag:100];
    NSInteger num = scroll.contentOffset.x / self.frame.size.width;
    NSInteger sum = scroll.contentSize.width / self.frame.size.width;
    
    self.labelOfPicNum.text = [NSString stringWithFormat:@"%ld/%ld",num + 1, sum];

}

- (void)dealloc {
    [_userView release];
    [_picsView release];
    [_messageView release];
    [_tagView release];
    [_likeUserView release];
    [_timeAndLookView release];
    [_viewOfUserFace release];
    [_labelOfAuthor release];
    [_labelOfAddress release];
    [_buttonOfisfollow release];
    [_labelOfPicNum release];
    [super dealloc];
}





@end
